<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h2>系统登录</h2>
      </div>
      
      <el-form :model="loginForm" ref="loginFormRef" :rules="rules" label-position="top" size="large">
        <el-form-item prop="phone" label="用户名">
          <el-input 
            v-model="loginForm.phone" 
            placeholder="请输入账号" 
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password" label="密码">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="Lock" 
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, inject } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const loginFormRef = ref(null);
const loading = ref(false);

// 尝试查找父组件提供的更新登录状态方法
const updateLoginState = inject('updateLoginState', null);

const loginForm = reactive({
  phone: '',
  password: '',
  remember: false
});

const rules = {
  phone: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
};

// 登录方法
const handleLogin = async () => {
  if (!loginFormRef.value) return;
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      
      try { 
        const response = await axios.post('http://10.137.10.169:8080/api/login', {
          phone: loginForm.phone,
          password: loginForm.password
        });
        
        if (response.data && response.data.code === 200) {
          const { data: userInfo, auth } = response.data;
          
          // 保存token
          localStorage.setItem('auth', auth);
          
          // 保存用户信息
          localStorage.setItem('isLoggedIn', 'true');
          localStorage.setItem('username', `${userInfo.major}-${userInfo.employeeId}`);
          localStorage.setItem('userRole', `a${userInfo.permissionLevel}`);
          
          // 如果有更新登录状态的方法，调用它
          if (updateLoginState) {
            updateLoginState(true, `${userInfo.major}-${userInfo.employeeId}`, `a${userInfo.permissionLevel}`);
          }
          
          ElMessage.success('登录成功');
          
          // 根据permissionLevel跳转到对应页面
          const targetPath = `/a${userInfo.permissionLevel}`;
          router.push({
            path: targetPath,
            replace: true
          });
        } else {
          throw new Error(response.data?.msg || '登录失败');
        }
      } catch (error) {
        ElMessage.error(error.message || '登录失败，请重试');
        console.error('登录错误:', error);
      } finally {
        loading.value = false;
      }
    } else {
      return false;
    }
  });
};
</script>

<style>
/* 重设App.vue的全局样式，解决在登录页面的问题 */
body.login-page {
  margin: 0;
  padding: 0;
  height: 100vh;
  background-color: #f0f2f5;
}
</style>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #1a365d 0%, #2c5282 100%);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 215, 0, 0.05) 0%, transparent 60%);
  animation: rotate 30s linear infinite;
  top: -50%;
  left: -50%;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.login-card {
  width: 480px;
  background: rgba(255, 255, 255, 0.98);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  padding: 48px 56px;
  margin: 0 auto;
  position: relative;
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: 1px solid rgba(255, 215, 0, 0.1);
}

.logo-container {
  width: 90px;
  height: 90px;
  margin: 0 auto 32px;
  background: linear-gradient(135deg, #1a365d 0%, #2c5282 100%);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(26, 54, 93, 0.2);
  transition: all 0.3s ease;
  border: 2px solid rgba(255, 215, 0, 0.2);
}

.logo-container i {
  font-size: 44px;
  color: #ffd700;
}

.login-header {
  text-align: center;
  margin-bottom: 48px;
}

.login-header h2 {
  color: #1a365d;
  font-size: 36px;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.5px;
  position: relative;
  padding-bottom: 16px;
}

.login-header h2::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 3px;
  background: linear-gradient(90deg, #1a365d, #ffd700);
  border-radius: 3px;
}

.subtitle {
  color: #4a5568;
  font-size: 18px;
  margin-top: 12px;
  font-weight: 400;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 18px;
  font-weight: 500;
  border-radius: 8px;
  margin-top: 24px;
  background: linear-gradient(135deg, #1a365d 0%, #2c5282 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(26, 54, 93, 0.3);
}

.login-button::after {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    transparent,
    rgba(255, 215, 0, 0.1),
    transparent
  );
  transition: 0.5s;
}

.remember-me {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
}

.forget-password {
  margin-left: auto;
  font-size: 14px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  font-size: 15px;
  color: #2d3748;
}

:deep(.el-input__wrapper) {
  height: 48px;
  padding: 0 16px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__inner) {
  font-size: 16px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409EFF inset;
}

:deep(.el-form--label-top .el-form-item__label) {
  padding: 0 0 8px;
}

:deep(.el-radio__label) {
  font-size: 14px;
}

:deep(.el-checkbox__label) {
  font-size: 14px;
}

@media (max-width: 480px) {
  .login-card {
    width: 90%;
    max-width: 480px;
    padding: 36px 24px;
  }
  
  .login-header h2 {
    font-size: 32px;
  }
  
  .logo-container {
    width: 80px;
    height: 80px;
    margin-bottom: 24px;
  }
  
  .logo-container i {
    font-size: 36px;
  }
}
</style> 